
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>termios --- POSIX 风格的 tty 控制 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="tty --- 终端控制功能" href="tty.html" />
    <link rel="prev" title="crypt --- Function to check Unix passwords" href="crypt.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/termios.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="tty.html" title="tty --- 终端控制功能"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="crypt.html" title="crypt --- Function to check Unix passwords"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="unix.html" accesskey="U">Unix 专有服务</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-termios">
<span id="termios-posix-style-tty-control"></span><h1><a class="reference internal" href="#module-termios" title="termios: POSIX style tty control. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">termios</span></code></a> --- POSIX 风格的 tty 控制<a class="headerlink" href="#module-termios" title="永久链接至标题">¶</a></h1>
<hr class="docutils" id="index-0" />
<p>此模块提供了针对tty I/O 控制的 POSIX 调用的接口。 有关此类调用的完整描述，请参阅 <em class="manpage">termios(3)</em> Unix 指南页。 它仅在当安装时配置了支持 POSIX <em>termios</em> 风格的 tty I/O 控制的 Unix 版本上可用。</p>
<p>此模块中的所有函数均接受一个文件描述符 <em>fd</em> 作为第一个参数。 这可以是一个整数形式的文件描述符，例如 <code class="docutils literal notranslate"><span class="pre">sys.stdin.fileno()</span></code> 所返回的对象，或是一个 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>，例如 <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code> 本身。</p>
<p>这个模块还定义了与此处所提供的函数一起使用的所有必要的常量；这些常量与它们在 C 中的对应常量同名。 请参考你的系统文档了解有关如何使用这些终端控制接口的更多信息。</p>
<p>这个模块定义了以下函数：</p>
<dl class="function">
<dt id="termios.tcgetattr">
<code class="sig-prename descclassname">termios.</code><code class="sig-name descname">tcgetattr</code><span class="sig-paren">(</span><em class="sig-param">fd</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcgetattr" title="永久链接至目标">¶</a></dt>
<dd><p>对于文件描述符 <em>fd</em> 返回一个包含 tty 属性的列表，形式如下: <code class="docutils literal notranslate"><span class="pre">[iflag,</span> <span class="pre">oflag,</span> <span class="pre">cflag,</span> <span class="pre">lflag,</span> <span class="pre">ispeed,</span> <span class="pre">ospeed,</span> <span class="pre">cc]</span></code>，其中 <em>cc</em> 为一个包含 tty 特殊字符的列表（每一项都是长度为 1 的字符串，索引号为 <code class="xref py py-const docutils literal notranslate"><span class="pre">VMIN</span></code> 和 <code class="xref py py-const docutils literal notranslate"><span class="pre">VTIME</span></code> 的项除外，这些字段如有定义则应为整数）。 对旗标和速度以及 <em>cc</em> 数组中索引的解读必须使用在 <a class="reference internal" href="#module-termios" title="termios: POSIX style tty control. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">termios</span></code></a> 模块中定义的符号常量来完成。</p>
</dd></dl>

<dl class="function">
<dt id="termios.tcsetattr">
<code class="sig-prename descclassname">termios.</code><code class="sig-name descname">tcsetattr</code><span class="sig-paren">(</span><em class="sig-param">fd</em>, <em class="sig-param">when</em>, <em class="sig-param">attributes</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcsetattr" title="永久链接至目标">¶</a></dt>
<dd><p>根据 <em>attributes</em> 列表设置文件描述符 <em>fd</em> 的 tty 属性，该列表即 <a class="reference internal" href="#termios.tcgetattr" title="termios.tcgetattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">tcgetattr()</span></code></a> 所返回的对象。 <em>when</em> 参数确定何时改变属性: <code class="xref py py-const docutils literal notranslate"><span class="pre">TCSANOW</span></code> 表示立即改变，<code class="xref py py-const docutils literal notranslate"><span class="pre">TCSADRAIN</span></code> 表示在传输所有队列输出后再改变，或 <code class="xref py py-const docutils literal notranslate"><span class="pre">TCSAFLUSH</span></code> 表示在传输所有队列输出并丢失所有队列输入后再改变。</p>
</dd></dl>

<dl class="function">
<dt id="termios.tcsendbreak">
<code class="sig-prename descclassname">termios.</code><code class="sig-name descname">tcsendbreak</code><span class="sig-paren">(</span><em class="sig-param">fd</em>, <em class="sig-param">duration</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcsendbreak" title="永久链接至目标">¶</a></dt>
<dd><p>在文件描述符 <em>fd</em> 上发送一个中断。 <em>duration</em> 为零表示发送时长为 0.25--0.5 秒的中断；<em>duration</em> 非零值的含义取决于具体系统。</p>
</dd></dl>

<dl class="function">
<dt id="termios.tcdrain">
<code class="sig-prename descclassname">termios.</code><code class="sig-name descname">tcdrain</code><span class="sig-paren">(</span><em class="sig-param">fd</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcdrain" title="永久链接至目标">¶</a></dt>
<dd><p>进入等待状态直到写入文件描述符 <em>fd</em> 的所有输出都传送完毕。</p>
</dd></dl>

<dl class="function">
<dt id="termios.tcflush">
<code class="sig-prename descclassname">termios.</code><code class="sig-name descname">tcflush</code><span class="sig-paren">(</span><em class="sig-param">fd</em>, <em class="sig-param">queue</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcflush" title="永久链接至目标">¶</a></dt>
<dd><p>在文件描述符 <em>fd</em> 上丢弃队列数据。 <em>queue</em> 选择器指定哪个队列: <code class="xref py py-const docutils literal notranslate"><span class="pre">TCIFLUSH</span></code> 表示输入队列，<code class="xref py py-const docutils literal notranslate"><span class="pre">TCOFLUSH</span></code> 表示输出队列，或 <code class="xref py py-const docutils literal notranslate"><span class="pre">TCIOFLUSH</span></code> 表示两个队列同时。</p>
</dd></dl>

<dl class="function">
<dt id="termios.tcflow">
<code class="sig-prename descclassname">termios.</code><code class="sig-name descname">tcflow</code><span class="sig-paren">(</span><em class="sig-param">fd</em>, <em class="sig-param">action</em><span class="sig-paren">)</span><a class="headerlink" href="#termios.tcflow" title="永久链接至目标">¶</a></dt>
<dd><p>在文件描述符 <em>fd</em> 上挂起一战恢复输入或输出。 <em>action</em> 参数可以为 <code class="xref py py-const docutils literal notranslate"><span class="pre">TCOOFF</span></code> 表示挂起输出，<code class="xref py py-const docutils literal notranslate"><span class="pre">TCOON</span></code> 表示重启输出，<code class="xref py py-const docutils literal notranslate"><span class="pre">TCIOFF</span></code> 表示挂起输入，或 <code class="xref py py-const docutils literal notranslate"><span class="pre">TCION</span></code> 表示重启输入。</p>
</dd></dl>

<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt>模块 <a class="reference internal" href="tty.html#module-tty" title="tty: Utility functions that perform common terminal control operations. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tty</span></code></a></dt><dd><p>针对常用终端控制操作的便捷函数。</p>
</dd>
</dl>
</div>
<div class="section" id="example">
<span id="termios-example"></span><h2>示例<a class="headerlink" href="#example" title="永久链接至标题">¶</a></h2>
<p>这个函数可提示输入密码并且关闭回显。 请注意其采取的技巧是使用一个单独的 <a class="reference internal" href="#termios.tcgetattr" title="termios.tcgetattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">tcgetattr()</span></code></a> 调用和一个 <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> ... <a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> 语句来确保旧的 tty 属性无论在何种情况下都会被原样保存:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">getpass</span><span class="p">(</span><span class="n">prompt</span><span class="o">=</span><span class="s2">&quot;Password: &quot;</span><span class="p">):</span>
    <span class="kn">import</span> <span class="nn">termios</span><span class="o">,</span> <span class="nn">sys</span>
    <span class="n">fd</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">fileno</span><span class="p">()</span>
    <span class="n">old</span> <span class="o">=</span> <span class="n">termios</span><span class="o">.</span><span class="n">tcgetattr</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
    <span class="n">new</span> <span class="o">=</span> <span class="n">termios</span><span class="o">.</span><span class="n">tcgetattr</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
    <span class="n">new</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">new</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">&amp;</span> <span class="o">~</span><span class="n">termios</span><span class="o">.</span><span class="n">ECHO</span>          <span class="c1"># lflags</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">termios</span><span class="o">.</span><span class="n">tcsetattr</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">termios</span><span class="o">.</span><span class="n">TCSADRAIN</span><span class="p">,</span> <span class="n">new</span><span class="p">)</span>
        <span class="n">passwd</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="n">prompt</span><span class="p">)</span>
    <span class="k">finally</span><span class="p">:</span>
        <span class="n">termios</span><span class="o">.</span><span class="n">tcsetattr</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">termios</span><span class="o">.</span><span class="n">TCSADRAIN</span><span class="p">,</span> <span class="n">old</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">passwd</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">termios</span></code> --- POSIX 风格的 tty 控制</a><ul>
<li><a class="reference internal" href="#example">示例</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="crypt.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code> --- Function to check Unix passwords</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="tty.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tty</span></code> --- 终端控制功能</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/termios.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="tty.html" title="tty --- 终端控制功能"
             >下一页</a> |</li>
        <li class="right" >
          <a href="crypt.html" title="crypt --- Function to check Unix passwords"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="unix.html" >Unix 专有服务</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>